﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ICertificate
    {
        public DataSet Certificate_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Certificate_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Certificate_GetWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Certificate_EmployeeID", EmployeeID);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Certificate_GetReport(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_GetReport", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Certificate_EmployeeID", EmployeeID);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public CertificateInfo Certificate_GetInfo(int CertificateID)
        {
            CertificateInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Certificate_ID", CertificateID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CertificateInfo();
                    retVal.ID = Convert.ToInt32(dr["Certificate_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["Certificate_EmployeeID"]);
                    retVal.Name = dr["Certificate_Name"].ToString();
                    retVal.AuthorityIssue = dr["Certificate_AuthorityIssue"].ToString();
                    retVal.IssueDate = dr["Certificate_IssueDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Certificate_IssueDate"]);
                    retVal.Grade = dr["Certificate_Grade"].ToString();
                    retVal.Speciality = dr["Certificate_Speciality"].ToString();
                    retVal.Description = dr["Certificate_Description"].ToString();
                    retVal.Kind = dr["Certificate_Kind"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Certificate_Insert(CertificateInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Certificate_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Certificate_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Certificate_AuthorityIssue", eInfo.AuthorityIssue);
            if (eInfo.IssueDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Certificate_IssueDate", eInfo.IssueDate);
            else
                dbCmd.Parameters.AddWithValue("@Certificate_IssueDate", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@Certificate_Grade", eInfo.Grade);
            dbCmd.Parameters.AddWithValue("@Certificate_Speciality", eInfo.Speciality);
            dbCmd.Parameters.AddWithValue("@Certificate_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@Certificate_Kind", eInfo.Kind);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Certificate_Update(CertificateInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Certificate_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Certificate_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Certificate_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Certificate_AuthorityIssue", eInfo.AuthorityIssue);
            if (eInfo.IssueDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Certificate_IssueDate", eInfo.IssueDate);
            else
                dbCmd.Parameters.AddWithValue("@Certificate_IssueDate", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@Certificate_Grade", eInfo.Grade);
            dbCmd.Parameters.AddWithValue("@Certificate_Speciality", eInfo.Speciality);
            dbCmd.Parameters.AddWithValue("@Certificate_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@Certificate_Kind", eInfo.Kind);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Certificate_UpdateEmployee(int EmployeeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_UpdateEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Certificate_Delete(int CertificateID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Certificate_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Certificate_ID", CertificateID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

